#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;

int n;
char s[N];
int a[N], tot, f[N][2];

void solve() {
	n = in, tot = 0; scanf("%s", s + 1);
	int cur = 0;
	rep(i, 1, n) {
		if(s[i] == '0') cur++;
		else a[++tot] = cur, cur = 0;
	} a[++tot] = cur;
	//rep(i, 1, tot) cerr << a[i] << " "; cerr << endl;
	f[0][0] = f[0][1] = 0;
	rep(i, 1, tot) {
		f[i][0] = f[i][1] = 1e9;
		rep(j, 0, 1) rep(k, 0, 1) {
			int ret = 0;
			if(j == 0 && k == 1) ret = (a[i] + 1) / 2 + 1;
			if(j == 1 || k == 0) ret = a[i] / 2  + 1;
			if(j == 1 && k == 0) ret = (a[i] + 1) / 2;
			if(i == 1) {
				if(k == 0) ret = a[i];
				else ret = a[i] + 1;
			}
			if(i == tot) {
				if(j == 1) ret = a[i];
				else ret = a[i] + 1;
			}
			if(a[i] == 0) ret = 0;
			chkmin(f[i][k], max(f[i - 1][j], ret));
		}
	}
	printf("%d\n", min(f[tot][0], f[tot][1]));
}

int main() {
#ifdef YJR_2333_TEST
	freopen("1.in", "r", stdin);
#endif
	int T = in;
	while(T--) {
		solve();
	}
	return 0;
}
